var layer;
var form;
var laySP;
$(function(){
	layui.use(['layer','form','upload'], function() {
		layer = layui.layer;
		form = layui.form;

		// 初始加载是否锁屏 ==========================================================================================================
		if(!sp){
			suoping();
		}

		// 提交锁屏 ==========================================================================================================
		form.on('submit(subSp)',function(data){
			subSP(1);
			return false;
		})

		// 提交修改密码 ==========================================================================================================
		form.on('submit(submitForm)',  function(data) {
			if(data.field['password1'].length < 6 || data.field['password2'].length < 6){
				layer.msg("密码长度必须为（6~18）");
			}else {
				if (data.field['password1'] != data.field['password2']){
					layer.msg("两次密码不一致");
				}else {
					if (data.field['oldPass'] == data.field['password2']){
						layer.msg("新密码不能和旧密码一样");
					}else {
						data.field['id'] = user_id;
						data.field['oldPass'] = aesMinEncrypt(data.field['oldPass']);
						data.field['password1'] = aesMinEncrypt(data.field['password1']);
						data.field['password2'] = aesMinEncrypt(data.field['password2']);
						var text = sbumitLoading('#role_menu_form .lay-submit-btn');
						getAjax(ctxPath+'main/updatePassWord',  data.field, 'post', function (res) {
							if (res.success) {
								layer.closeAll();
							}
							showTips(res.msg);
							closeLoading('#role_menu_form .lay-submit-btn', text)
						})
					}
				}
			}
			return false;
		})

	});

	// 删除选中操作 ==========================================================================================================
	$('.layui-layout-right dd').click(function(){
		$(this).removeClass('layui-this');
	})

	// 全屏/退出全屏 ==========================================================================================================
	$('.screen').click(function(){
		var id = $(this).attr('lay-id');
		if(id == 0){
			fullScreen();
			$(this).attr({'lay-id': "1", "title": "退出全屏"});
			$(this).find(".layui-icon").html('&#xe758;');
		}else{
			 exitFullscreen();
			$(this).attr({'lay-id': "0", "title": "全屏"});
			$(this).find(".layui-icon").html('&#xe622;');
		}
	})

	// 锁屏 ==========================================================================================================
	$('#lockScreen').click(function(){
		suoping();
		subSP();
	})



})


/**
 * ------------------------------------------------------------------------------------------------------------------------------------------
 * 加载错误照片
 * @param $obj
 */
function errorImg($obj, key) {
	var value = $($obj).attr("key-id");
	if((!value || value == 0) && !key){
		$($obj).attr({"src": "images/head.png", "onerror": "", "key-id": "1"});
	}else if (key && key == 1){
		//$($obj).attr({"src": "images/head2.png", "onerror": "", "key-id": "1"});
	}
}

/**
 * ------------------------------------------------------------------------------------------------------------------------------------------
 * 加载完成关闭圈圈
 */
window.onload=function(){
	$(".lay-loader").fadeOut();
	setTimeout(function(){
		$(".lay-loader").remove();
	}, 1000)
}

/**
 * ------------------------------------------------------------------------------------------------------------------------------------------
 * 基本信息
 */
function updatePassWord(id){
	var data = {"id": id, "name": user_name};
	openWindow('password_form', "修改密码", '400px', '350px', false, 0, false, false, data, 0.2, false, function () {
		$("#password_form").find("input[name=name]").attr("readonly", true);
	})
}

/**
 * ------------------------------------------------------------------------------------------------------------------------------------------
 * 锁屏
 */
function suoping() {
	$('#suoping').val('');
	laySP = layer.open({
		title : '请输入登录密码解锁',
		type : 1,
		shade : 0.2,
		//skin : 'layui-layer-molv',// 样式类名
		closeBtn : 0,
		area : ['380px','110px'],
		btn : false,
		resize : false, // 禁止拉伸
		content : $('#div_sp'),
		time : 0,
		success : function(){
			layer.msg("锁屏中",{offset: "20%"});
		}
	});
}

/**
 * ------------------------------------------------------------------------------------------------------------------------------------------
 * 锁屏校验
 */
function subSP(mark) {
	var obj = {};
	if(mark){
		var val = $('#suoping').val();
		if(!val){
			return layer.msg("请输入密码",{offset:"20%"});
		}
		obj = {'suoping': aesMinEncrypt(val), 'mark': 1}
	}
	$.post(ctxPath+'main/suoping', obj, function(res) {
		if (res.success) {
			sp = res.data;
			layer.close(laySP);
		}
		layer.msg(res.msg, {offset:"25%"});
	})
}

/**
 * ------------------------------------------------------------------------------------------------------------------------------------------
 * 退出登录
 */
function exitLogin(){
	window.location.href = "loginExit";
}

/**
 * ------------------------------------------------------------------------------------------------------------------------------------------
 * 清除缓存
 */
function clearCache() {
	$.post(ctxPath+'main/clearCache', {}, function(res) {
		layer.msg(res.msg, {offset:"25%"});
	})
}

/**
 * ------------------------------------------------------------------------------------------------------------------------------------------
 * 大数据界面
 */
function showData() {
	window.open(ctxPath+'views/data/face', '_blank');
}

/**
 * ------------------------------------------------------------------------------------------------------------------------------------------
 * 全屏
 */
function fullScreen() {
    var element = document.documentElement;
    if (element.requestFullscreen) {
        element.requestFullscreen();
    } else if (element.msRequestFullscreen) {
        element.msRequestFullscreen();
    } else if (element.mozRequestFullScreen) {
        element.mozRequestFullScreen();
    } else if (element.webkitRequestFullscreen) {
        element.webkitRequestFullscreen();
    }
}

/**
 * ------------------------------------------------------------------------------------------------------------------------------------------
 * 退出全屏
 */
function exitFullscreen() {
    if (document.exitFullscreen) {
        document.exitFullscreen();
    } else if (document.msExitFullscreen) {
        document.msExitFullscreen();
    } else if (document.mozCancelFullScreen) {
        document.mozCancelFullScreen();
    } else if (document.webkitExitFullscreen) {
        document.webkitExitFullscreen();
    }
}

/**
 * ------------------------------------------------------------------------------------------------------------------------------------------
 * 监听window是否全屏，并进行相应的操作,支持esc键退出
 */
window.onresize = function() {
    var isFull=!!(document.webkitIsFullScreen || document.mozFullScreen || 
        document.msFullscreenElement || document.fullscreenElement
    );//!document.webkitIsFullScreen都为true。因此用!!
    if (isFull==false) {
		$(".screen").attr({'lay-id': "0", "title": "全屏"});
		$(".screen i").html('&#xe622;');
	}else{
		$(".screen").attr({'lay-id': "1", "title": "退出全屏"});
		$(".screen i").html('&#xe758;');
	}
}






